# app/routes/file.py
from flask import Blueprint, jsonify, request
from app.utils.security import token_required
from app.services.music_service import MusicService
from app.models import Playlist
from app.utils.response_utils import (
    success_response,
    error_response,
    ResponseCode
)

# 创建蓝图对象
music_bp = Blueprint('musics', __name__)


@music_bp.route('/getPlayLists', methods=['POST'])
@token_required
def get_play_lists(currentUserId):
    result = MusicService.get_play_lists(user_id=currentUserId)

    if isinstance(result, tuple):  # 如果是错误响应
        return error_response(
            code=ResponseCode.PERMISSION_DENIED,
            message=result[0],
            http_code=result[1]
        )

    return success_response(
        data=result,
        message=f'success'
    )

@music_bp.route('/addPlayList', methods=['POST'])
@token_required
def add_play_list(currentUserId):
    data = request.get_json()
    name = data.get('name')

    if not name:
        return error_response(code=ResponseCode.PARAM_ERROR,message='参数错误',http_code=403)

    result = MusicService.add_play_list(user_id=currentUserId,name=name)

    if isinstance(result, tuple):  # 如果是错误响应
        return error_response(
            code=ResponseCode.PERMISSION_DENIED,
            message=result[0],
            http_code=result[1]
        )

    return success_response(
        data=result,
        message=f'创建歌单成功'
    )

@music_bp.route('/editPlayList', methods=['POST'])
@token_required
def edit_play_list(currentUserId):
    data = request.get_json()
    name = data.get('name')
    cover_url = data.get('cover_url')
    descripe = data.get('descripe')
    songs = data.get('songs')
    playlist_id = data.get('playlist_id')

    if not playlist_id:
        return error_response(code=ResponseCode.PARAM_ERROR,message='参数错误',http_code=403)

    result = MusicService.edit_play_list(user_id=currentUserId,playlist_id=playlist_id,name=name,cover_url=cover_url,descripe=descripe,songs=songs)

    if isinstance(result, tuple):  # 如果是错误响应
        return error_response(
            code=ResponseCode.PERMISSION_DENIED,
            message=result[0],
            http_code=result[1]
        )

    return success_response(
        data=result,
        message=f'编辑歌单成功'
    )
